package com.ilibrary.web.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.ilibrary.base.MyAjaxAction;
import com.ilibrary.service.BookService;
import com.ilibrary.service.CategoryService;
import com.ilibrary.utils.MyTools;
import com.ilibrary.web.dto.ResultDto;
import com.ilibrary.web.vo.BookVO;

public class DoQueryBookListAction extends MyAjaxAction {

	private BookService bookService;
	private CategoryService categoryService;
	
	public BookService getBookService() {
		return bookService;
	}
	public void setBookService(BookService bookService) {
		this.bookService = bookService;
	}
	public CategoryService getCategoryService() {
		return categoryService;
	}
	public void setCategoryService(CategoryService categoryService) {
		this.categoryService = categoryService;
	}

	@Override
	protected JSONObject doExecute(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws Exception {
		
		String categoryId = request.getParameter("categoryId");
		String searchCondition = request.getParameter("searchCondition");
		
		int start = Integer.parseInt(request.getParameter("start"));
		int limit = Integer.parseInt(request.getParameter("limit"));
		
		JSONObject jo = new JSONObject();
		JSONArray ja = new JSONArray();
		JSONObject joo = null;
		
		ResultDto<BookVO> resultDto = bookService.queryList(start, limit, categoryId, searchCondition);
		List<BookVO> bookVOList = resultDto.getResultList();
		
		for (BookVO bookVO : bookVOList) {
			joo = new JSONObject();
			joo.put("bookId", bookVO.getBookId());
			joo.put("categoryId", bookVO.getCategoryId());
			joo.put("categoryName", bookVO.getCategoryName());
			joo.put("bookName", bookVO.getBookName());
			joo.put("bookAuthor", bookVO.getBookAuthor());
			joo.put("bookPrice", bookVO.getBookPrice());
			joo.put("bookPubDt", bookVO.getBookPubDt().toString());
			joo.put("bookPubHouse", bookVO.getBookPubHouse());
			ja.add(joo);
		}
		jo.put("data", ja);
		jo.put("total", resultDto.getTotal());
		return jo;
	}

}
